package palmtop.com.palmtop.activity;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.multidex.MultiDex;

import com.nostra13.universalimageloader.cache.memory.MemoryCache;
import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.tencent.bugly.Bugly;

import net.grandcentrix.tray.AppPreferences;

import org.litepal.LitePalApplication;
import org.xutils.x;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/20.
 * 自定义application
 */
public class MyApplication extends Application {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private static MyApplication instance;
    private List<Activity> activitys = null;
    private List<Activity> activitylist = null;
    public static DisplayImageOptions mNormalImageOptions;
    public AppPreferences appPreferences;
    Context context;
    public MyApplication() {
        super();
        this.activitys = new LinkedList<Activity>();
        this.activitylist = new LinkedList<Activity>();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        //TypefaceProvider.registerDefaultIconSets();
        LitePalApplication.initialize(this);
        x.Ext.init(this);
        x.Ext.setDebug(true);
        initImageLoader(this);
        Bugly.init(getApplicationContext(), "29fbf19f49", true);
//        attachBaseContext(this);
    }

    public void addActivity(Activity activity) {
        if (activitys != null && activitys.size() > 0) {
            if(!activitys.contains(activity)){
                activitys.add(activity);
            }
        }else{
            activitys.add(activity);
        }
    }

    public void exit() {
        if (activitys != null && activitys.size() > 0) {
            for (Activity activity : activitys) {
                activity.finish();
            }
        }
        //System.exit(0);
    }

//退出登录处理activity
    public void addActivityList(Activity activity) {
        if (activitylist != null && activitylist.size() > 0) {
            if(!activitylist.contains(activity)){
                activitylist.add(activity);
            }
        }else{
            activitylist.add(activity);
        }
    }
//退出登录关闭之前所有的activity
    public void exitlist() {
        if (activitylist != null && activitylist.size() > 0) {
            for (Activity activity : activitylist) {
                activity.finish();
            }
        }
    }

    public static MyApplication getInstance() {
        if (null == instance) {
            instance = new MyApplication();
        }
        return instance;

    }

    //setAppPreference
    public void setAppPreference(String loginname,String name,String userid,String ssgrid,String gridname){
        appPreferences = new AppPreferences(getApplicationContext());
        appPreferences.put("loginname", loginname);
        appPreferences.put("name", name);
        appPreferences.put("userid", userid);
        appPreferences.put("ssgrid", ssgrid);
        appPreferences.put("gridname", gridname);
    }

    /*public void setSharedPreference(String loginname,String name,String userid,String ssgrid,String gridname) {
        sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        editor.putString("loginname", loginname);
        editor.putString("name", name);
        editor.putString("userid", userid);
        editor.putString("ssgrid", ssgrid);
        editor.putString("gridname", gridname);
       // editor.commit();// 提交修改
        editor.apply();
    }*/

    public static void initImageLoader(Context context) {
        int memoryCacheSize = (int) (Runtime.getRuntime().maxMemory() / 5);
        MemoryCache memoryCache;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            memoryCache = new LruMemoryCache(memoryCacheSize);
        } else {
            memoryCache = new LRULimitedMemoryCache(memoryCacheSize);
        }

        mNormalImageOptions = new DisplayImageOptions.Builder().bitmapConfig(Bitmap.Config.RGB_565).cacheInMemory(true).cacheOnDisc(true)
                .resetViewBeforeLoading(true).build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).defaultDisplayImageOptions(mNormalImageOptions)

                .memoryCache(memoryCache)
                // .memoryCacheSize(memoryCacheSize)
                .tasksProcessingOrder(QueueProcessingType.LIFO).threadPriority(Thread.NORM_PRIORITY - 2).threadPoolSize(3).build();

        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this) ;
    }
}
